<template>
  <el-dialog :title="formValidate.id ? $t('collectionBusiness.edit') : $t('collectionBusiness.add')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('collectionBusiness.tabData.name')" prop="phaseName">
        <el-input
          v-model="formValidate.phaseName"
          clearable
          :maxlength="50"
          :placeholder="$t('collectionBusiness.prompt.name')"
        />
      </el-form-item>
      <el-form-item :label="$t('collectionBusiness.tabData.fromTime')" prop="startOverdueTime">
        <el-input-number
          v-model="formValidate.startOverdueTime"
          controls-position="right"
          :step="1"
          :max="10000"
          :placeholder="$t('collectionBusiness.prompt.fromTime')"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item :label="$t('collectionBusiness.tabData.toTime')" prop="endOverdueTime">
        <el-input-number
          v-model="formValidate.endOverdueTime"
          controls-position="right"
          :step="1"
          :max="10000"
          :placeholder="$t('collectionBusiness.prompt.toTime')"
          style="width: 100%"
        />
      </el-form-item>

      <el-form-item :label="$t('collectionBusiness.tabData.flowMode')" prop="flowPattern">
        <el-select v-model="formValidate.flowPattern" clearable :placeholder="$t('collectionBusiness.prompt.flowMode')">
          <el-option
            v-for="item in $t('collectionBusiness.flowMode')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="retainedDays">
        <template slot="label">
          <el-checkbox v-model="formValidate.dayModel">
            {{ $t('collectionBusiness.tabData.days') }}
          </el-checkbox>
        </template>
        <el-input-number
          v-model="formValidate.retainedDays"
          controls-position="right"
          :step="1"
          :max="10000"
          :placeholder="$t('collectionBusiness.prompt.days')"
          :disabled="!formValidate.dayModel"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item prop="loanTermWithoutReminder">
        <template slot="label">
          <el-checkbox v-model="formValidate.freeModel">
            {{ $t('collectionBusiness.tabData.freeLoanTerm') }}
          </el-checkbox>
        </template>
        <el-row>
          <el-col :span="7">
            <el-select v-model="formValidate.select" clearable :placeholder="$t('collectionBusiness.prompt.select')" :disabled="!formValidate.freeModel">
              <el-option
                v-for="item in $t('collectionBusiness.operator')"
                :key="item.value"
                :label="item.desc"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="14" :offset="1">
            <el-input-number
              v-model="formValidate.loanTermWithoutReminder"
              controls-position="right"
              :step="1"
              :max="10000"
              :placeholder="$t('collectionBusiness.prompt.freeLoanTerm')"
              :disabled="!formValidate.freeModel"
              style="width: 100%"
            />
          </el-col>
          <el-col :span="2">
            <span style="margin-left: 10px">{{ $t('collectionBusiness.day') }}</span>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item :label="$t('collectionBusiness.tabData.state')" prop="state">
        <el-select v-model="formValidate.state" clearable :placeholder="$t('collectionBusiness.prompt.state')">
          <el-option
            v-for="item in $t('collectionBusiness.state')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addBusinessConfig, updateBusinessConfig } from '@/api/collection'
export default {
  name: 'BusinessConfigDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        phaseName: null,
        startOverdueTime: null,
        endOverdueTime: null,
        flowPattern: null,
        retainedDays: null,
        loanTermWithoutReminder: null,
        select: null,
        state: null,
        dayModel: null,
        freeModel: null
      },
      rules: {
        phaseName: [{ required: true, message: this.$t('collectionBusiness.tabData.name') + this.$t('common.null'), trigger: 'blur' }],
        startOverdueTime: [{ required: true, message: this.$t('collectionBusiness.tabData.fromTime') + this.$t('common.null'), trigger: 'blur' }],
        endOverdueTime: [{ required: true, message: this.$t('collectionBusiness.tabData.toTime') + this.$t('common.null'), trigger: 'blur' }],
        flowPattern: [{ required: true, message: this.$t('collectionBusiness.tabData.flowMode') + this.$t('common.null'), trigger: 'blur' }],
        state: [{ required: true, message: this.$t('collectionBusiness.tabData.state') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = {
            id: this.formValidate['id'],
            phaseName: this.formValidate['phaseName'],
            startOverdueTime: this.formValidate['startOverdueTime'],
            endOverdueTime: this.formValidate['endOverdueTime'],
            flowPattern: this.formValidate['flowPattern'],
            retainedDays: this.formValidate.dayModel ? this.formValidate['retainedDays'] : 0,
            loanTermWithoutReminder: (this.formValidate.freeModel && this.formValidate['select']) ? (this.formValidate['select'] + this.formValidate['loanTermWithoutReminder']) : 0,
            state: this.formValidate['state']
          }
          if (data['id']) {
            updateBusinessConfig(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addBusinessConfig(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
.admin-tree{
  margin-top: 20px;
}
</style>

